package study.datastructure.leetcode.javase.primary.mutithreads;/**
 * @program: datastructure
 * @author: lcy
 * @create: 2024-09-08 00:19
 */

import com.sun.corba.se.spi.ior.ObjectKey;

/**
 2024/9/8,
 */


public class Client {
    static final Object lock = new Object();
    static int number=0;
    private static void write(int change) {
        synchronized (lock) {
            number += change;
        }
    }
    private static void read() {
        System.out.println("number = " + number);
    }
    public static void main(String[] args) throws InterruptedException {
        // 开启一个线程加 10000 次
        new Thread(() -> {
            for (int i = 0; i < 10000; i++) {
                write(1);
            }
            System.out.println("增加 10000 次已完成");
        }).start();

        // 开启一个线程减 10000 次
        new Thread(() -> {
            for (int i = 0; i < 10000; i++) {
                write(-1);
            }
            System.out.println("减少 10000 次已完成");
        }).start();

        // 睡眠一秒保证线程执行完成
        Thread.sleep(1000);
        // 读取结果
        read();
    }
}
